<?php

/*============================================
Classe Concert
============================================*/

class Concert extends ModelObject{

	protected static $cached_object_list= array();
	protected $dateConcert;
	protected $nom;
	protected $racineUserInstrumentConcert;
	protected $isTemplate;

	protected function __construct( $dateConcert,$nom,$racineUserInstrumentConcert,$isTemplate){
		try{
			
			$this->dateConcert = $dateConcert;
			$this->nom = $nom;
			$this->racineUserInstrumentConcert = $racineUserInstrumentConcert;
			$this->isTemplate = $isTemplate;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getDateConcert(){
		return $this->dateConcert;
	}

	public function getNom(){
		return $this->nom;
	}

	public function getRacineUserInstrumentConcert(){
		return UserInstrumentConcert::get($this->racineUserInstrumentConcert);
	}

	public function getIsTemplate(){
		return $this->isTemplate;
	}

	public function setDateConcert( $dateConcert){
		try{
			$temp = $this->dateConcert;
			if($this->dateConcert !== $dateConcert){
				$this->dateConcert = $dateConcert;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->dateConcert = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDateConcert() a échouée",0,$e);
		}
	}

	public function setNom( $nom){
		try{
			$temp = $this->nom;
			if($this->nom !== $nom){
				$this->nom = $nom;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->nom = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setNom() a échouée",0,$e);
		}
	}

	public function setRacineUserInstrumentConcert( $racineUserInstrumentConcert){
		try{
			$temp = $this->racineUserInstrumentConcert;
			if(is_null($racineUserInstrumentConcert)){
				if($this->racineUserInstrumentConcert !== $racineUserInstrumentConcert){
					$this->racineUserInstrumentConcert = null;
					$this->update();
				}
			}
			else if(!($racineUserInstrumentConcert instanceof UserInstrumentConcert) || is_null($racineUserInstrumentConcert->getId())){
				throw new Exception("Le paramètre \$racineUserInstrumentConcert de type get_class($racineUserInstrumentConcert) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->racineUserInstrumentConcert !== $racineUserInstrumentConcert->getId()){
					$this->racineUserInstrumentConcert = $racineUserInstrumentConcert->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->racineUserInstrumentConcert = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setRacineUserInstrumentConcert() a échouée",0,$e);
		}
	}

	public function setIsTemplate( $isTemplate){
		try{
			$temp = $this->isTemplate;
			if($this->isTemplate !== $isTemplate){
				$this->isTemplate = $isTemplate;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->isTemplate = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setIsTemplate() a échouée",0,$e);
		}
	}

	public static function create( $dateConcert,$nom,$racineUserInstrumentConcert,$isTemplate){
		try{
			$concert = new Concert($dateConcert, $nom, $racineUserInstrumentConcert, $isTemplate);
			$concert->insert();
			Concert::cacheObject($concert);
			return $concert;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Concert a échouée",0,$e);
		}
	}

	public static function get( $concert){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($concert) || $concert === "") {
				return null;
			}
			if(Concert::existsInCache($concert)) {
				return Concert::getFromCache($concert);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `concert` WHERE `concert` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$concert));
			if ($row = $stmt->fetch()){
				$concert = new Concert( $row['dateConcert'],$row['nom'],$row['racineUserInstrumentConcert'],$row['isTemplate']);
				$concert->id =  $row['concert'];
				$dbh->commit();
				Concert::cacheObject($concert);
				return $concert;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $concert . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `concert` SET `dateConcert` = :dateConcert, `nom` = :nom, `racineUserInstrumentConcert` = :racineUserInstrumentConcert, `isTemplate` = :isTemplate WHERE `concert` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':dateConcert'=>$this->dateConcert, ':nom'=>$this->nom, ':racineUserInstrumentConcert'=>$this->racineUserInstrumentConcert, ':isTemplate'=>$this->isTemplate, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `concert` WHERE `concert` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Concert::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `concert` ( `dateConcert`, `nom`, `racineUserInstrumentConcert`, `isTemplate`) VALUES (:dateConcert, :nom, :racineUserInstrumentConcert, :isTemplate)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':dateConcert'=>$this->dateConcert, ':nom'=>$this->nom, ':racineUserInstrumentConcert'=>$this->racineUserInstrumentConcert, ':isTemplate'=>$this->isTemplate));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `concert`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Concert::existsInCache($row['concert'])){
					$obj = Concert::getFromCache($row['concert']);
				}
				else{
					$obj = new Concert( $row['dateConcert'],$row['nom'],$row['racineUserInstrumentConcert'],$row['isTemplate']);
				}
				if(!is_null($obj)){
					$obj->id = $row['concert'];
					Concert::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Concert a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `concert`';
			$sql_select = 'SELECT concert FROM `concert`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Concert::getObjectFromCache($row['concert']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Concert::removeObjectFromCache($row['concert']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Concert a échouée",0,$e);
		}
	}

	/*fonction de jointure*/
	public function getListPartitionWhereConcert( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `id_partition` FROM `partition`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `id_concert` = :id';
			}
			else{
				$sql .= $where.' AND `id_concert` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = Partition::get($row['id_partition']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type Partition liés à l'objet de type Concert ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	/*fonction de jointure*/
	public function getListUserinstrumentconcertWhereconcertId( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `userInstrumentConcert` FROM `userinstrumentconcert`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `concert_id` = :id';
			}
			else{
				$sql .= $where.' AND `concert_id` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = Userinstrumentconcert::get($row['userInstrumentConcert']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type Userinstrumentconcert liés à l'objet de type Concert ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

}